package com.buddies.lottery.data;

import com.buddies.lottery.data.repo.IMRepo;
import com.buddies.lottery.data.repo.LotteryRepo;
import com.buddies.lottery.data.repo.MessageRepo;
import com.buddies.lottery.data.repo.UserRepo;

import java.lang.ref.SoftReference;

/**
 * Description:
 * Date: 2018/2/23
 * Author: xuliang
 */
public class DataManager {

    private SoftReference<UserRepo> mUserRepo;
    private SoftReference<LotteryRepo> mLotteryRepo;
    private SoftReference<MessageRepo> mMessageRepo;
    private SoftReference<IMRepo> mIMRepo;

    private DataManager() {
    }

    private static class SingletonHolder {
        private static final DataManager INSTANCE = new DataManager();
    }

    public static DataManager getInstance() {
        return SingletonHolder.INSTANCE;
    }

    /**
     * 用户
     */
    public UserRepo getUserRepo() {
        if (mUserRepo == null || mUserRepo.get() == null) {
            UserRepo loader = new UserRepo();
            mUserRepo = new SoftReference<>(loader);
        }
        return mUserRepo.get();
    }

    /**
     * 游戏
     */
    public LotteryRepo getLotteryRepo() {
        if (mLotteryRepo == null || mLotteryRepo.get() == null) {
            LotteryRepo loader = new LotteryRepo();
            mLotteryRepo = new SoftReference<>(loader);
        }
        return mLotteryRepo.get();
    }

    /**
     * 消息
     */
    public MessageRepo getMessageRepo() {
        if (mMessageRepo == null || mMessageRepo.get() == null) {
            MessageRepo messageRepo = new MessageRepo();
            mMessageRepo = new SoftReference<>(messageRepo);
        }
        return mMessageRepo.get();
    }

    /**
     * IM
     */
    public IMRepo getIMRepo() {
        if (mIMRepo == null || mIMRepo.get() == null) {
            IMRepo repo = new IMRepo();
            mIMRepo = new SoftReference<>(repo);
        }
        return mIMRepo.get();
    }

}
